from MsgProcess import MsgProcess, MsgType
import time,os,re
from threading import Thread
import plugin.Video.url as url

class Video(MsgProcess):
    key=0#正在播放的索引

    def process(self):
        taskcmd = 'ps ax | grep vlc' 
        out = os.popen(taskcmd).read()             
        pat = re.compile(r'(\d+).+(vlc)')
        return pat.findall(out)

    #唤醒暂停
    def Awake(self, message=None):
        for x in self.process():
            os.system("sudo kill -STOP "+x[0])        


    def Text(self, message):  
        data = message["Data"]
        songnames = [x for x in url.url]

        if isinstance(data, dict):
            if data['initstate'] == 'onLoad':
                data = {"action": "vedio","playindex": self.key, 'playlist': songnames} 
                self.send(MsgType.Text, Receiver='MqttProxy', Data=data)
            return 1

        else:

            if "切换" in data:
                self.key=int( data[2:] )
                self.send(MsgType=MsgType.Text, Receiver="Screen", Data="播放视频"+songnames[self.key])
                self.send(MsgType=MsgType.Text, Receiver="SpeechSynthesis", Data="播放视频"+songnames[self.key])  
                for x in self.process():
                    os.system("sudo kill -9 "+x[0])  
                                                        # 'mplayer -fs '+url.url[songnames[self.key]]
                Thread(  target= lambda : os.system(   'sudo  su pi  -c " vlc  --no-video-title-show  -f --play-and-exit {0}"'.format( url.url[songnames[self.key]] )  )    ).start()
                data = {"action": "vedio","playindex": self.key, 'playlist': songnames} 
                self.send(MsgType.Text, Receiver='MqttProxy', Data=data)
                return 1

            if "播放视频" in data or "播放节目" in data or "打开视频" in data:
                data = data[4:].lower()
                temporary=[x  for x in url.url if x in data ]
                if  temporary:
                    
                    self.send(MsgType=MsgType.Text, Receiver="Screen", Data="播放视频"+data)
                    self.send(MsgType=MsgType.Text, Receiver="SpeechSynthesis", Data="播放视频"+data)  
                    for x in self.process():
                        os.system("sudo kill -9 "+x[0])  

                    self.key = songnames.index(temporary[0])
                    Thread(  target= lambda : os.system(   'sudo  su pi  -c " vlc  --no-video-title-show  -f --play-and-exit {0}"'.format( url.url[temporary[0]])   )    ).start()
                    data = {"action": "vedio","playindex": self.key, 'playlist': songnames} 
                    self.send(MsgType.Text, Receiver='MqttProxy', Data=data)
                else:
                    self.send(MsgType=MsgType.Text, Receiver="Screen", Data="没有发现该节目")
                    self.send(MsgType=MsgType.Text, Receiver="SpeechSynthesis", Data="没有发现该节目")  
                return 1

            if "暂停视频" in data:
                for x in self.process():
                    os.system("sudo kill -STOP "+x[0])
                self.say("视频已暂停")
                return 1

            if "继续视频" in data:
                for x in self.process():
                    os.system("sudo kill -CONT "+x[0])
                self.say("继续视频")
                return 1

            if "关闭视频" in data:
                for x in self.process():
                    os.system("sudo kill -9 "+x[0])
                self.say("视频已关闭")
                return 1

            if "节目列表" in data:
                for x,y in zip(url.url,[  x for x in range( 1,len(url.url)+1 ) ] ):
                    self.say("节目{}:{}".format(y,x))
                return 1